import $service from '../../../../service.js';
var prescription_id;
var prescriptionNo;
var paymentMethod = 2
let inquiryType = ''
const app = getApp();
let ishospitalPre = ''
Page({
  data: {
    hospitalType: $service.hospitalType,
    isShow: false,
    "active": true,
    prescriptionMap: {},
    doctorMap: {},
    inquiryMap: {},
    prescriptionlist: [],
    prescription_imgurl: '',
    isPay: true, //是否支付
    isGetOrder: "1", //是否生成订单
    pay_type: 3, //付费类型
    syncFlag: false,
    state: '',
    prescription_id: "",
    prescriptionNo: "",
    ishospitalPre: ""
  },
  onLoad(e) {
    prescription_id = e.prescription_id
    prescriptionNo = e.prescriptionNo
    paymentMethod = e.paymentMethod
    ishospitalPre = e.ishospitalPre
    inquiryType = e.inquiryType
    this.setData({
      state: e.state,
      prescription_id: e.prescription_id,
      prescriptionNo: e.prescriptionNo,
      ishospitalPre: e.ishospitalPre
    })
    this.getEstablishPdf();
  },
  onShow() {
    this.getIsPrescriptionOrder()
  },


  // 获取处方图片
  getEstablishPdf() {
    wx.showLoading({
      title: '图片加载中',
    })
    let params = {
      "id": prescription_id,
      "resourceType": 4,
      "type": 2
    }
    app.http.getEstablishPdf(params).then(res => {
      this.setData({
        prescription_imgurl: res.data.url
      })
    }).catch(err => {
      $service.showToast(err.msg);
    }).finally(res => {
      wx.hideLoading();
    })
  },
  previewImage: function (e) { //图片放大
    var index = e.currentTarget.dataset.index;
    var num = e.currentTarget.dataset.num;

    var all = [];
    all.push(this.data.prescription_imgurl);
    all.forEach(item => {
      item = item.replace(/https/, 'http')
    })
    wx.previewImage({
      urls: all
    })
  },
  guidanceMapTap(e) {
    var num = e.currentTarget.dataset.num;
    if (num == 1) {
      if (this.data.prescription_imgurl) {
        wx.showLoading({
          title: '图片保存中',
        })
        wx.downloadFile({
          url: this.data.prescription_imgurl,
          success: function (res) {
            console.log(res);
            //图片保存到本地
            wx.saveImageToPhotosAlbum({
              filePath: res.tempFilePath,
              success: function (data) {
                wx.showToast({
                  title: '保存成功',
                  icon: 'success',
                  duration: 2000
                })
              },
              fail: function (err) {
                console.log(err);
                if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
                  console.log("当初用户拒绝，再次发起授权")
                  wx.openSetting({
                    success(settingdata) {
                      console.log(settingdata)
                      if (settingdata.authSetting['scope.writePhotosAlbum']) {
                        console.log('获取权限成功，给出再次点击图片保存到相册的提示。')
                      } else {
                        console.log('获取权限失败，给出不给权限就无法正常使用的提示')
                      }
                    }
                  })
                }
              },
              complete(res) {
                wx.hideLoading({})
              }
            })
          },
          fail:(e) => {
            console.log('错误', e)
          }
        })
      } else {
        $service.showToast('未生成成功')
      }
    } else {

      if (paymentMethod == 2 && this.data.hospitalType == 'zhuhai') {
        //医保跳转(珠海的才要)
        wx.navigateToMiniProgram({
          appId: 'wx55bd4c7d421e388d',
          path: 'pages/homeYaofang/homeYaofang',
          // extraData: {
          //   foo: 'bar'
          // },
          // envVersion: 'develop',
          success(res) {
            // 打开成功
          }
        })
      } else {
        if (inquiryType == 5) {
          this.setData({
            isShow: true
          })
          return
        }
        if (this.data.isGetOrder == "1") {
          wx.navigateTo({
            url: '../../../prescription_flow/order_create/order_create?prescription_id=' + prescription_id + '&prescriptionNo=' + prescriptionNo + '&ishospitalPre=' + ishospitalPre
          })
        } else {
          wx.navigateTo({
            url: '../../../prescription_flow/order_msg/order_msg?prescription_id=' + prescription_id
          })
        }
      }
    }
  },
  // 查看是否已经生成订单
  getIsPrescriptionOrder() {
    app.http.getIsPrescriptionOrder({
      prescriptionId: prescription_id
    }).then(res => {
      if (res.data.orderSn || ($service.hospitalType == 'lianjiang' && res.data.isStart == 1)) {
        this.setData({
          isPay: false
        })
      }
    })
  },
  // 同步处方
  //     syncBtn (e) {
  //         wx.showLoading({
  //             title: '加载中',
  //         })
  //         let type = e.currentTarget.dataset.type;
  //         let jsonData ={
  //           "orgid": $service.orgid, 
  //           "actid": type === 'cancel'?'cxpressync':"dopressync",
  //           "order_id" : prescription_id,
  //           "rand": Math.random()
  //         }
  //         wx.request({
  //             url:  "https://zz.nxk520.com/nethosp/service/nethosp_interface.ashx",
  //             method: 'GET',
  //             data: jsonData,
  //             dataType: "json",
  //             complete: (res) => {
  //                 wx.hideLoading();
  //                 $service.showToast(res.data.msg);
  //             }
  //         });
  // }

})